﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using WoSoft.PACS.Data.DBProvider;
using WoSoft.PACS.Data.Models;

namespace WoSoft.PACS.Data.ModelsExt
{
    /// <summary>
    /// 数据源扩展类
    /// </summary>
    public static class DataSourceModelExtend
    {
        /// <summary>
        /// 根据数据源查询返回内容
        /// </summary>
        /// <param name="dmodel"></param>
        /// <param name="sqlText"></param>
        /// <returns></returns>
        public static DataTable QueryTable(this DataSourceModel model, string sqlText)
        {
            if (model != null)
            {
                var dbType = model.DatabaseType;
                if (dbType == 1)//oracle数据源
                {
                    using (OracleHelper oracleHelper = new OracleHelper(model.ServerIP, model.Instance, model.UserName, model.Password))
                    {
                        return oracleHelper.QueryText(sqlText, null);
                    }
                }
                else if (dbType == 2)//Sql server数据源
                {
                    using (SqlServerHelper sqlHelper = new SqlServerHelper(model.ServerIP, model.Instance, model.DatabaseName, model.UserName, model.Password))
                    {
                        return sqlHelper.QueryTable(sqlText, null);
                    }
                }
                else if (dbType == 3)//Sybase 数据源
                {
                    using (ODBCHelper odbcHelper = new ODBCHelper(model.ServerIP, model.Instance, model.UserName, model.Password))
                    {
                        return odbcHelper.QueryText(sqlText, null);
                    }
                }
            }

            return null;
        }


    }
}
